/**
 * Created by Administrator on 17-6-9.
 */
$(function() {
    var ajItem = $(".ajlist");
    var t = new Date().getTime();
    ajItem.each(function(){
        var _this = $(this);
        var ajUrl = _this.attr('l') || false;
        if(!ajUrl) return false; else ajUrl =  ajUrl + '?t=' + t;
        var ajLoadingStr = '<p class="loading"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i><span class="sr-only">Loading...</span></p>';
        var ajShowLoading = _this.attr('showLoading') || false;
        if(ajShowLoading){
            _this.html(ajLoadingStr);
        }
        $.ajax({
            type: "GET",
            url: ajUrl,
            dataType:'json',
            success: function (data) {
                if(data.status){
                    _this.html(data.html);
                }else{
                    alert(data.msg);
                }

            },
            error : function (data){

            }
        });
    });
    //限时秒杀
    $('.week_tab').on('click','li',function(){
        var index = $(this).index();
        if(!$(this).hasClass('active')){
            $(this).addClass('active').siblings().removeClass('active');
            $('.week_list').eq(index).addClass('active').siblings().removeClass('active');
        }
    })
    $('.seckill_tab').on('click','li',function(){
        var index = $(this).index();
        if(!$(this).hasClass('active')){
            $(this).addClass('active').siblings().removeClass('active');
            $('.seckill_list').eq(index).addClass('active').siblings().removeClass('active');
        }
    })
    $.ajax({
        type: "GET",
        //url: 'resource/test_seckill.json',
        url: CONFIG_EDU_URLS["ajax_url"]+'home/ajax_spike',
        dataType:'json',
        success: function (data) {
            if(data.status && data.result.count>0){
                var seckillstra = '';
                var seckillstrb = '';
                var mclass;
                data.result.list.forEach(function(item,index){
                    mclass = '';
                    if(index<3){
                        mclass = item.task_is_member_only == '1' ? 'active' : '' ;
                        seckillstra += '<li class="seckill_item"><a href="'+item.link+'" target="_blank" title="'+item.name+'"><img src="'+item.pic+'" alt="'+item.name+'"><span class="membertag '+mclass+'">会员尊享</span><div class="title ellipsis-2">'+item.name+'</div><div class="price"><em class="sale_price">￥'+item.promotion+'</em><del class="market_price">￥'+item.market_price+'</del><span class="seckillp">立即<br>抢购</span></div></a></li>';
                    }else if (index>2 && index<6){
                        mclass = item.task_is_member_only == '1' ? 'active' : '' ;
                        seckillstrb += '<li class="seckill_item"><a href="'+item.link+'" target="_blank" title="'+item.name+'"><img src="'+item.pic+'" alt="'+item.name+'"><span class="membertag '+mclass+'">会员尊享</span><div class="title ellipsis-2">'+item.name+'</div><div class="price"><em class="sale_price">￥'+item.promotion+'</em><del class="market_price">￥'+item.market_price+'</del><span class="seckillp">立即<br>抢购</span></div></a></li>';

                    }
                });
                var listcount = data.result.count;
                $('.seckill_lista').html(seckillstra);
                if(listcount>=4){
                    $('.seckill_listb').html(seckillstrb);
                    $('.seckill_tab ul').append('<li></li>');
                }
                imgload();
            }else{
                void 0;
            }
        },
        error : function (data){
        }
    });
    function imgload(){
        $('.second_kill_wrap img.loading').each(function(){
            var url = $(this).attr('src');
            var _this = $(this);
            loadImage(url,function(){
                _this.parent().find('.default').attr('src',url);
            })
        });
    }
    function loadImage(url, callback) {
        var img = new Image(); //创建一个Image对象，实现图片的预下载
        img.onload = function(){
            img.onload = null;
            callback();
        }
        img.src = url;
    }
    var count = 0;
    function countdown(){
        count++;
        $('.countdown').each(function(){
            var timeleft = parseInt($(this).data('endtime'));
            updateTime((timeleft*1000-count*10),$(this));
        });
    }
    function updateTime(ts,ele){
        var d, h, m, s;
        d = parseInt(ts / 1000 / 60 / 60 / 24), //总毫秒除以一天的毫秒 得到相差的天数  
        h = parseInt(ts / 1000 / 60 / 60 - (24 * d)), //然后取完天数之后的余下的毫秒数再除以每小时的毫秒数得到小时 
        m = parseInt(ts / 1000 / 60 - (24 * 60 * d) - (60 * h)), //减去天数和小时数的毫秒数剩下的毫秒，再除以每分钟的毫秒数，得到分钟数 
        s = parseInt(ts / 1000 - (24 * 60 * 60 * d) - (60 * 60 * h) - (60 * m)); //得到最后剩下的毫秒数除以1000 就是秒数，再剩下的毫秒自动忽略即可
        hm = Math.floor((ts - (24 * 60 * 60 * 1000 * d) - (60 * 60 * 1000 * h) - (60 * 1000 * m) - (s * 1000)) / 10);
        h<10?h='0'+h:'';
        m<10?m='0'+m:'';
        s<10?s='0'+s:'';
        parseInt(d)>0?h = parseInt(d)*24+parseInt(h):'';
        ts>0?ele.html('<em class="hour">'+h+'</em><em class="min">'+m+'</em><em class="sec">'+s+'</em><em class="msec">'+hm+'</em>'):ele.html('<em class="killend">秒杀结束</em>');
    }
});
